package com.beamfield.controller.manage.material;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.PurchasePlan;
import com.beamfield.modelVo.*;
import com.beamfield.service.manage.material.OutBoundService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 出库管理
 */
@Controller
@RequestMapping("/cms/material/outbound")
public class OutBoundController extends BaseController {

    private final OutBoundService outBoundService;


    @Autowired
    public OutBoundController(OutBoundService outBoundService) {
        this.outBoundService = outBoundService;
    }

    @RequestMapping(value = "/outbound",method = RequestMethod.GET)
    public ModelAndView outbound(){
        return new ModelAndView();
    }
    @RequestMapping(value = "/outboundDetail",method = RequestMethod.GET)
    public ModelAndView outboundDetail(){
        return new ModelAndView();
    }
    /**
     * 获取材料以及材料出库数量
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSuppliesByType",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSuppliesByType(HttpServletRequest request){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        Long beamId = super.getBeamId(request);
        String type = request.getParameter("type");
        if(StringUtils.isEmpty(type)){
            return apiResult;
        }
        List<SuppliesOutboundVo> suppliesByType = null;
        try {
            suppliesByType = outBoundService.getSuppliesByType(type, beamId);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        if(suppliesByType!=null && !suppliesByType.isEmpty()) {
            apiResult.setCode(HttpResultEnum.SUCCESS.getCode());
            apiResult.setMessage(HttpResultEnum.SUCCESS.getMessage());
            apiResult.setResult(suppliesByType);
        }
        return apiResult;
    }

    /**
     * 获取出库详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/readOuboundData",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readOuboundData(HttpServletRequest request){
        ApiResult apiResult = new ApiResult();
        String project_id = request.getParameter("project_id");
        String issueId = request.getParameter("issueId");
        String projectDetailId = request.getParameter("projectDetailId");
        String supplies_id = request.getParameter("supplies_id");
        if(StringUtils.isEmpty(project_id)||StringUtils.isEmpty(issueId)||StringUtils.isEmpty(supplies_id)){
            apiResult.setCode(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        Long beamId = super.getBeamId(request);
        List<OutboundVo> outboundVoList = null;
        long total = 0L;
        try {
            outboundVoList = outBoundService.readOuboundData(beamId, project_id, issueId, projectDetailId, supplies_id);
            //total = outBoundService.countIssueDataAndAlreadyOut(beamId, suppliesMaterialName, useDep);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        apiResult.setResult(outboundVoList);
        return apiResult;
    }

    /**
     * 获取发料详情和已发料数量
     * @param request
     * @return
     */
    @RequestMapping(value = "/readIssueData",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readIssueData(HttpServletRequest request){
        ApiResult apiResult = new ApiResult();
        String suppliesMaterialName = request.getParameter("supplies_material_name");
        if(StringUtils.isEmpty(suppliesMaterialName)){
            apiResult.setCode(HttpResultEnum.MIS_PARAM.getCode());
            apiResult.setMessage(HttpResultEnum.MIS_PARAM.getMessage());
            return apiResult;
        }
        String issueId = request.getParameter("issueId");
//        String useDep = request.getParameter("useDep");
//        String offset = request.getParameter("offset");
//        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        List<IssueVo> issueVoList = null;
        long total = 0l;
        try {
            issueVoList = outBoundService.readIssueDataAndAlreadyOut(beamId, suppliesMaterialName, issueId, null, null);
            //total = outBoundService.countIssueDataAndAlreadyOut(beamId, suppliesMaterialName, useDep);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        apiResult.setResult(issueVoList);
        return apiResult;
    }

    /**
     * 获取采购计划，下拉框用
     * @param request
     * @return
     */
    @RequestMapping(value = "/readPurchaseSelect",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readPurchaseSelect(HttpServletRequest request){
        String projectId = request.getParameter("projectId");
        if(StringUtils.isEmpty(projectId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        List<PurchasePlan> purchasePlanList = outBoundService.readPurchaseSelect(projectId);
        return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"获取成功", purchasePlanList);
    }

    /**
     * 获取钢筋入库信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readRebarData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readRebarData(HttpServletRequest request){
        String suppliesId = request.getParameter("supplies_id");
        String projectId = request.getParameter("project_id");
        Long beamId = super.getBeamId(request);
        if(StringUtils.isEmpty(suppliesId)||StringUtils.isEmpty(projectId)){
            return null;
        }
        String purchasePlanId = request.getParameter("purchase_plan_id");
        List<OutboundRebarVo> rebarOutboundVoList = outBoundService.getRebarData(suppliesId, projectId, purchasePlanId, beamId);
        return new PageResult(rebarOutboundVoList.size(),0, rebarOutboundVoList);
    }

    /**
     * 获取预埋件入库信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readEmbedData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readEmbedData(HttpServletRequest request){
        String suppliesId = request.getParameter("supplies_id");
        String projectId = request.getParameter("project_id");
        if(StringUtils.isEmpty(suppliesId)||StringUtils.isEmpty(projectId)){
            return null;
        }
        String purchasePlanId = request.getParameter("purchase_plan_id");
        List<OutboundEmbedVo> outboundEmbedVoList = outBoundService.getEmbedData(suppliesId, projectId, purchasePlanId);
        return new PageResult(outboundEmbedVoList.size(),0, outboundEmbedVoList);
    }

    /**
     * 普通出库
     * @param request
     * @return
     */
    @RequestMapping(value = "/commOutbound",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult commOutbound(HttpServletRequest request){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String issueId = request.getParameter("issueId");
        String projectId = request.getParameter("projectId");
        String projectDetailId = request.getParameter("projectDetailId");
        String outboundNum = request.getParameter("outboundNum");
        String suppliesId = request.getParameter("suppliesId");
        if(StringUtils.isEmpty(issueId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(projectId)){
            return apiResult;
        }
//        if(StringUtils.isEmpty(projectDetailId)){
//            return apiResult;
//        }
        if(StringUtils.isEmpty(outboundNum)|| !CommonUtils.isNumber(outboundNum)){

            return apiResult;
        }
        if(StringUtils.isEmpty(suppliesId)){
            return apiResult;
        }
        try{
            apiResult = outBoundService.commOutbound(issueId, projectDetailId, projectId, CommonUtils.parseDouble(outboundNum, 0), suppliesId, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }

    /**
     * 预埋件出库
     * @param request
     * @return
     */
    @RequestMapping(value = "/embedOutbound",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult embedOutbound(HttpServletRequest request){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String issueId = request.getParameter("issueId");
        String projectId = request.getParameter("projectId");
        String projectDetailId = request.getParameter("projectDetailId");
        String suppliesId = request.getParameter("suppliesId");
        String embedInbound = request.getParameter("embedInbound");
        if(StringUtils.isEmpty(issueId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(projectId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(projectDetailId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(suppliesId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(embedInbound)){
            return apiResult;
        }
        try{
            apiResult = outBoundService.embedOutbound(issueId, projectId, projectDetailId, suppliesId, embedInbound, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }
    /**
     * 钢筋出库
     * @param request
     * @return
     */
    @RequestMapping(value = "/rebarOutbound",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult rebarOutbound(HttpServletRequest request){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String issueId = request.getParameter("issueId");
        String projectId = request.getParameter("projectId");
        String projectDetailId = request.getParameter("projectDetailId");
        String suppliesId = request.getParameter("suppliesId");
        String rebarInbound = request.getParameter("rebarInbound");
        if(StringUtils.isEmpty(issueId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(projectId)){
            return apiResult;
        }
//        if(StringUtils.isEmpty(projectDetailId)){
//            return apiResult;
//        }
        if(StringUtils.isEmpty(suppliesId)){
            return apiResult;
        }
        if(StringUtils.isEmpty(rebarInbound)){
            return apiResult;
        }
        try{
            apiResult = outBoundService.rebarOutbound(issueId, projectId, projectDetailId, suppliesId, rebarInbound, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }


}
